﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics;
using FarseerPhysics.Factories;

namespace RacePlatformer.Object
{
    public class Monster : GameObject
    {
        private enum MonsterState { moving, idle }
        private MonsterState currentMonsterState = MonsterState.moving;
        private enum moveDirection { RIGHT, LEFT }
        private moveDirection currentMoveDiretion = moveDirection.LEFT;
        /// <summary>
        /// Object which functions as a monster
        /// </summary>
        /// <param name="maingame">The 'MainGame.cs' class that needs to be passed</param>
        /// <param name="world">The world class that needs to be passed, is required to create objects and fixtures</param>
        /// <param name="size">the size of the object</param>
        /// <param name="position">the position of the object(in normal units)</param>
        /// <param name="bodytype">the bodytype of the object</param>
        /// <param name="mass">the mass of the object</param>
        /// 
        private float moveBorderLeft;
        private float moveBorderRight;
        public Monster(MainGame maingame, World world, Vector2 position, int levelID)
            : base(maingame, world, position, levelID)
        {
            this.body = BodyFactory.CreateRectangle(world, UnitsConverter.ToSimUnits(mainAnimation.frameWidth), UnitsConverter.ToSimUnits(mainAnimation.frameHeight), 1f, UnitsConverter.ToSimUnits(position));
            this.body.BodyType = BodyType.Dynamic;
            this.layer = 0.45f;
            this.scale = 1.0f;
            this.levelID = levelID;
            this.body.CollisionGroup = Manager.CollisionGroupManager.MONSTER;
            this.body.FixedRotation = true;
            this.body.Friction = 0.0f;
            this.body.Restitution = 0f;
            this.body.LinearDamping = 5.0f;
            this.layer = 0.49f;
            moveBorderLeft = UnitsConverter.ToDisplayUnits(this.body.Position.X) - 100;
            moveBorderRight = UnitsConverter.ToDisplayUnits(this.body.Position.X) + 100;
        }
        public override void Update(GameTime gametime)
        {
            mainAnimation.update(gametime);
            switch (currentMonsterState)
            {
                case MonsterState.idle:
                    break;
                case MonsterState.moving:
                    switch (currentMoveDiretion)
                    {
                        case moveDirection.LEFT:
                            if (UnitsConverter.ToDisplayUnits(this.body.Position.X) > moveBorderLeft)
                            {
                                this.body.LinearVelocity = new Vector2(-2, 0);
                            }
                            else
                            {
                                this.desiredSpriteEffect = SpriteEffects.FlipHorizontally;
                                currentMoveDiretion = moveDirection.RIGHT;
                            }
                            break;
                        case moveDirection.RIGHT:
                            if (UnitsConverter.ToDisplayUnits(this.body.Position.X) < moveBorderRight)
                            {
                                this.body.LinearVelocity = new Vector2(2, 0);
                            }
                            else
                            {
                                this.desiredSpriteEffect = SpriteEffects.None;
                                currentMoveDiretion = moveDirection.LEFT;
                            }
                            break;
                    }
                    break;
            }
        }
        public override void LoadContent()
        {
            this.mainAnimation = new Manager.Animation(maingame, "Objects/monster/Hedgehog_Walking", 9, 197, 120, TimeSpan.FromSeconds(0.05f), Manager.Animation.AnimationType.contious);
        }
    }
}
